package com.kakaxi.mybilibili.module.home;

import android.util.SparseArray;

import com.kakaxi.mybilibili.module.home.advice.view.AdviceHomeFragment;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.module.home.category.view.CategoryHomeFragment;
import com.kakaxi.mybilibili.module.home.consider.view.ConsiderHomeFragment;
import com.kakaxi.mybilibili.module.home.drama.view.DramaHomeFragment;
import com.kakaxi.mybilibili.module.home.find.view.FindHomeFragment;
import com.kakaxi.mybilibili.module.home.live.view.LiveHomeFragment;

/**
 * 主界面的碎片工厂类
 * Created by kakaxi on 16/8/25.
 */

public class MainFragmentFactory {

    /** 碎片散列数组 **/
    private static SparseArray<BaseFragment> mFragments = new SparseArray<>();

    /** 工厂中碎片的数量 **/
    public static final int FRAGMENT_COUNT = 6;

    /**
     * 通过工厂模式返回对应位置的碎片
     *
     * @param position 位置
     * @return
     */
    public static BaseFragment createFragment(int position) {
        BaseFragment fragment = mFragments.get(position);
        if (fragment == null) {
            // 创建对应位置的碎片
            switch (position) {
                case 0: { // 直播
                    fragment = LiveHomeFragment.newInstance();
                    break;
                }
                case 1: { // 推荐
                    fragment = AdviceHomeFragment.newInstance();
                    break;
                }
                case 2: { // 番剧
                    fragment = DramaHomeFragment.newInstance();
                    break;
                }
                case 3: { // 分类
                    fragment = CategoryHomeFragment.newInstance();
                    break;
                }
                case 4: { // 关注
                    fragment = ConsiderHomeFragment.newInstance();
                    break;
                }
                case 5: { // 发现
                    fragment = FindHomeFragment.newInstance();
                    break;
                }

            }
            if (fragment != null) {
                // 把新建的碎片放入数组中
                mFragments.put(position, fragment);
            }
        }
        return fragment;
    }

}
